
"""
Create/read windows shortcuts.
"""

from win32com.shell import shell
import pythoncom, os

class WinShortcut:
    def __init__(self):
        self._instance = pythoncom.CoCreateInstance(
            shell.CLSID_ShellLink, None,
            pythoncom.CLSCTX_INPROC_SERVER,
            shell.IID_IShellLink
        )
        
    def load(self, filename):
        query = self._instance.QueryInterface(pythoncom.IID_IPersistFile)
        query.Load(filename)
        
    def save(self, filename):
        query = self._instance.QueryInterface(pythoncom.IID_IPersistFile)
        query.Save(filename, 0)

    def __getattr__(self, name):
        if name != "_instance":
            return getattr(self._instance, name)




if __name__ == '__main__':
    import sys

    path = "test.lnk"
    
    print "Create %r..." % path
    shortcut = WinShortcut()
    shortcut.SetPath("C:\\")
    shortcut.SetDescription("a test shortcut.")
    shortcut.SetWorkingDirectory("c:\\")
    shortcut.save(path)
    print "created, ok."


    print "\nread existing shortcut %r:" % path
    shortcut = WinShortcut()    
    shortcut.load(path)
    print "path.........:", shortcut.GetPath(shell.SLGP_UNCPRIORITY)[0]
    print "arguments....:", shortcut.GetArguments()
    print "description..:", shortcut.GetDescription()
    print "working dir..:", shortcut.GetWorkingDirectory()

    
    print "\ndelete %r" % path
    os.remove(path)

